package com.itheima.service.impl;

import com.itheima.common.utils.DateUtils;
import com.itheima.mapper.OrderSettingMapper;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    OrderSettingMapper orderSettingMapper;

    @Override
    public void editNumberByOrderDate(OrderSetting orderSetting) throws Exception {
        String s = DateUtils.parseDate2String(orderSetting.getOrderDate());
        orderSetting.setOrderDate(DateUtils.parseString2Date(s));
        orderSettingMapper.editNumberByOrderDate(orderSetting);
    }

    @Override
    public List<Map> getOrderSettingByMonth(String date) throws Exception {
        Date begin = DateUtils.parseString2Date(date + "-1");
        Date end = DateUtils.parseString2Date(date + "-31");
        Map map = new HashMap();
        map.put("begin", begin);
        map.put("end", end);
        List<OrderSetting> list = orderSettingMapper.getOrderSettingByMonth(map);
        List<Map> arrayList = new ArrayList<>();
        if (list != null && list.size() > 0) {
            for (OrderSetting orderSetting : list) {
                HashMap hashMap = new HashMap();
                hashMap.put("date", orderSetting.getOrderDate().getDate());
                hashMap.put("number", orderSetting.getNumber());
                hashMap.put("reservations", orderSetting.getReservations());
                arrayList.add(hashMap);
            }
        }
        return arrayList;
    }

    /**
     * 文件上传
     * @param orderSettings
     */
    @Override
    public void add(ArrayList<OrderSetting> orderSettings) {
        if (orderSettings != null && orderSettings.size() > 0 ){
            for (OrderSetting orderSetting : orderSettings) {
                long count = orderSettingMapper.findCountByOrderDate(orderSetting.getOrderDate());
                if (count > 0){
                    orderSettingMapper.editNumberByOrderDate(orderSetting);
                }else {
                    orderSettingMapper.add(orderSetting);
                }
            }
        }
    }
}
